﻿/******************************************************************************
 *  PawnShop.UI.DDDW.JS  [written by zhouqd] 
 *  publish date: 2009-7-6 (V0.1) 
 *****************************************************************************/
/******************************************************************************
 *  定义树状下拉列表中的一行
 */
PawnShop.NameSpace("PawnShop.UI");

/*************************************************************************** */
/* Tree_Node definition                                                      */
/* 包括操纵结点的方法，设置结点的文本、图标、对象、复选框、按钮、子结点、    */
/* 展开结点、选择结点的复选框、选择结点、取消选择结点、结点的Click事件、     */
/*************************************************************************** */
//初始化一行并设置行的属性
function TableRow(list,text,img_normal,tip_text,status,action)
{
    this._list        = list;
    this._next        = null; 
	this._prev        = null;
	this._parent      = null;
	this._frist_child = null;
	this._last_child  = null;
	this._child_count = 0;
	this._is_expanded = false;
	this._is_selected = false;
	this._is_checked  = false;
	
	this._img_normal = (typeof(img_normal)=='undefined'?"":img_normal);
	this._img_expanded = (typeof(img_expanded)=='undefined'?"":img_expanded);
	this._text = (typeof(text)=='undefined'?"":text);
	this._tip_text=(typeof(tip_text)=='undefined'?"":tip_text);
	this._status=(typeof(status)=='undefined'?"":status);

	this.id=Tree_Node_Index;
	Tree_Node_Array[Tree_Node_Index++]=this; //把节点存入Tree_Node_Array（节点数组）
	this.action=action;
	
    this.IsIndexColumn = (typeof(isIndex)=='undefined' ? true : isIndex);
	this.text=(typeof(text)=='undefined'?"":text);
	this.tip_text=(typeof(tip_text)=='undefined'?"":tip_text);
	this.status=(typeof(status)=='undefined'?"":status);

	this.id=Tree_Node_Index;
	Tree_Node_Array[Tree_Node_Index++]=this; //把节点存入Tree_Node_Array（节点数组）
	this.action=action;
	
}
/*
 * 的到节点对应的对象
 */
TableRow.prototype.Get_List = function ()
{
	return this._list;
}
/*
 * 得到结点的子结点
 */
TableRow.prototype.Get_Child = function (index)
{
	var child = this._first_child;
	var i=0;
	while(child && i < index)
	{
		child = child._next;i++;
	}
	return child;
}
/*
 * 展开结点
 */
TableRow.prototype.Expand = function (mode)
{
	mode = typeof(mode)=='undefined' ? 'auto' : mode;
	var tr = get_obj('DropDownListRow_'+this.id);
	var list = this.Get_List();
	if(!this._is_expanded && typeof(list.cb_expanding)=='function')
	{
		if(false==list.cb_expanding(this)) return;//cancel expand
	}
	if(this.is_expanded && typeof(list.cb_collapsing)=='function')
	{
		if(false==list.cb_collapsing(this)) return;//cancel collapse
	}
	if(this._child_count == 0) this._is_expanded=false;
	else if(mode == 'true' || mode == true) this._is_expanded=true;
	else if(mode == 'false'|| mode == false) this._is_expanded=false;
	else if(mode == 'auto') this._is_expanded =! _this.is_expanded;
	else return;

	if (tr) tr.style.display = this.is_expanded ? "block":"none";
	
//	var img = this.get_img_obj();
//	if(img) img.src=Tree_imgSrc(this);
//	var button=this.get_button_obj();
//	if(button)button.src=Tree_GetButtonImg(this);
	
	if(this.is_expanded && typeof(tree.cb_expanded)=='function')  tree.cb_expanded(this);
	if(!this.is_expanded && typeof(tree.cb_collapsed)=='function')  tree.cb_collapsed(this);
}
/*
 * 选择结点
 */
TableRow.prototype.Select=function ()
{
	var list = this.Get_List();
	if(list._selected_row && list._selected_row != this) list._selected_node.UnSelect();
	this._is_selected = true;
	list._selected_node = this;
//	var label = this.get_label_obj();
//	if(label && tree.style & ENUM_HIGHLIGHT_SELECT)
//	{
//		label.className=this.child_count>0?tree.parentnode_class_selected:tree.childnode_class_selected;
//	}
}
/* ------------------------------------------------------------------------- */
/* 取消选择结点                                                              */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.unselect=function ()
{
	var tree=this.get_tree();
	this.is_selected = false;
	if(tree.selected_node == this) tree.selected_node =null;
	var label = this.get_label_obj();
	if(label && tree.style & ENUM_HIGHLIGHT_SELECT)
	{
		label.className=this.child_count>0?tree.parentnode_class_normal:tree.childnode_class_normal;
	}
}
/* ------------------------------------------------------------------------- */
/* 设置结点的文本                                                            */
/* ------------------------------------------------------------------------- */
TableCell.prototype.Set_Text = function(text)
{
	this.text = text;
	var tree=this.get_tree();
	var obj=this.get_label_obj(); //得到节点对应的HTML标签对象
	
	if(obj) obj.innerHTML=(Tree_FormatText(this.text));
}
/* ------------------------------------------------------------------------- */
/* 得到结点对应的HTML的标签对象                                              */
/* ------------------------------------------------------------------------- */
TableRow.prototype.Get_Label_Obj = function ()
{
	return get_obj('Tree_label_'+this.id);
}
/* ------------------------------------------------------------------------- */
/* 得到结点的图标对象                                                        */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.get_img_obj = function ()
{
	return get_obj('Tree_img_'+this.id);
}
/* ------------------------------------------------------------------------- */
/* 得到结点的复选框对象                                                      */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.get_checkbox_obj = function ()
{
	return get_obj('Tree_checkbox_'+this.id);
}
/* ------------------------------------------------------------------------- */
/* 得到结点的按钮对象                                                        */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.get_button_obj = function ()
{
	return get_obj('Tree_button_'+this.id);
}


/* ------------------------------------------------------------------------- */
/* 选择结点的复选框                                                          */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.check = function (mode)
{
	mode = typeof(mode)=='undefined'?'auto':mode;
	if(mode == 'true'||mode==true)this.is_checked=true;
	else if(mode == 'false'||mode==false)this.is_checked=false;
	else if(mode == 'auto')this.is_checked=!this.is_checked;
	else return;
	var obj=this.get_checkbox_obj();
	if(obj)obj.checked=this.is_checked;
}

/* ------------------------------------------------------------------------- */
/* 结点的Click事件                                                           */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.on_click=function (hittest,event)
{
	var tree=this.get_tree();
	if(hittest==HT_BUTTON)
	{
		if(tree.behavior & ENUM_EXPAND_BUTTON) this.expand('auto');
		if(tree.behavior & ENUM_ACTION_BUTTON) this.do_action();
		if(this.__need_load && (tree.behavior & ENUM_LOAD_BUTTON)) tree.helper_dynamic_load(this);
	}
	if(hittest==HT_LABEL)
	{
		this.select();
		if(tree.behavior & ENUM_EXPAND_LABEL) this.expand('auto');
		if(tree.behavior & ENUM_ACTION_LABEL) this.do_action();
		if(this.__need_load && (tree.behavior & ENUM_LOAD_LABEL) ) tree.helper_dynamic_load(this);
	}
	if(hittest==HT_IMG)
	{
		this.select();
		if(tree.behavior & ENUM_EXPAND_IMG) this.expand('auto');
		if(tree.behavior & ENUM_ACTION_IMG) this.do_action();
		if(this.__need_load && (tree.behavior & ENUM_LOAD_IMG) )tree.helper_dynamic_load(this);
	}
	if(hittest==HT_CHECKBOX)
	{
		this.check('auto');
	}
}
/* ------------------------------------------------------------------------- */
/* 结点的MouseHover事件                                                      */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.on_mousehover=function (hittest,event)
{
	var tree=this.get_tree();
	if(hittest == HT_LABEL)
	{
		var label = this.get_label_obj();
		if(label==null)return;
		if(Tree_isIE)label.style.cursor='hand';
		if(Tree_isNC6)label.style.cursor='pointer';
		if(this.child_count>0)
			label.className=tree.parentnode_class_hover;
		else
    		label.className=tree.childnode_class_hover;
		if(tree.style & ENUM_SHOW_STATUS)
		{
			window.status=((tree.style & ENUM_USETEXTASSTATUS==0) ? this.status:(this.status!=""?this.status:this.text));
		}
		label.title=((tree.style &ENUM_SHOW_TOOLTIP) ==0 ? "":(tree.style & ENUM_USETEXTASTIP ==0 ? this.tip_text:(this.tip_text==""?this.text:this.tip_text)));
	}
	if(hittest == HT_BUTTON)
	{
		var button=this.get_button_obj();
		if(button==null) return;
		if(this.child_count == 0 && button) button.style.cursor='default';
		if(this.child_count>0 && button )
		{
			if(Tree_isIE)button.style.cursor='hand';
			if(Tree_isNC6)button.style.cursor='pointer';
		}
	}
	if(hittest == HT_IMG)
	{
		var img = this.get_img_obj();
		if(img==null)return;
		if(Tree_isIE)img.style.cursor='hand';
		if(Tree_isNC6)img.style.cursor='pointer';
	}
}
/* ------------------------------------------------------------------------- */
/* 结点的MouseOut事件                                                        */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.on_mouseout=function (hittest,event)
{
	var tree=this.get_tree();
	if(hittest == HT_LABEL)
	{
		var label = this.get_label_obj();
		if(label===null)return;
		if((tree.style & ENUM_HIGHLIGHT_SELECT) && this==tree.selected_node)
			label.className = this.child_count>0?tree.parentnode_class_selected:tree.childnode_class_selected;
		else if(this.child_count>0) label.className=tree.parentnode_class_normal;
		else label.className=tree.childnode_class_normal;
	}
}
/* ------------------------------------------------------------------------- */
/* 加入一个字节点                                                            */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.cb__add_child = function (child_node)
{
	var tree = this.get_tree();
	if(tree.__auto_draw == false) return;
	var img = this.get_img_obj();
	if(img) img.src=Tree_imgSrc(this);
	var button=this.get_button_obj();
	if(button) button.src=Tree_GetButtonImg(this);
	var parent=get_obj("Tree_expand_"+ this.id);
	if(parent==null)parent=get_obj("Tree_View_"+ tree.id);
	var div=document.createElement("DIV");
	div.innerHTML=Tree_table(child_node)+"<DIV id=Tree_expand_"+ child_node.id+
        " STYLE='{display:"+(child_node.is_expanded?"block;":"none;")+"}'></DIV>";
	var before=null;
	if(this.child_count==1);
	else if(this.last_child == child_node)
	{
		var node=child_node.prev;
		button=node.get_button_obj();
		if(button)
		{
			button.src=Tree_GetButtonImg(node);
			var line=document.getElementsByName("Tree_tdline_"+ node.id);
			for(var i=line.length-1;i>=0;i--)
				line[i].innerHTML="<IMG  src="+tree.img_list.item('line_I') + ">";
		}
	}
	else
	{
		button=child_node.next.get_button_obj();
		if(button)button.src=Tree_GetButtonImg(child_node.next);
		before=get_obj("Tree_expand_"+child_node.next.id).parentNode;
	}
	parent.insertBefore(div,before);
	child_node.unselect();
	if(this.is_selected)this.select();else this.unselect();
}
/* ------------------------------------------------------------------------- */
/* 删除一个字节点                                                            */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.cb__del_child = function (child_node)
{
	var tree = this.get_tree();
	if(tree.__auto_draw == false) return;
	if(tree.selected_node && child_node.contain(tree.selected_node)) tree.selected_node=null;
	var div=get_obj("Tree_expand_"+child_node.id).parentNode;
	div.parentNode.removeChild(div);
	if(child_node.__is_first==true)
	{
		var img = this.get_img_obj();
		if(img) img.src=Tree_imgSrc(this);
	 }
	else if(child_node.__is_last==true)
	{
		var node=child_node.prev;
		var line=document.getElementsByName("Tree_tdline_"+ node.id);
		for(var i=line.length-1;i>=0;i--)
			line[i].innerHTML="<IMG style='width:"+tree.LINE_WIDTH+";height:"+tree.LINE_HEIGHT+";visibility:hidden'>";
		var button=get_obj("Tree_button_"+node.id);
		if(button)  button.src=Tree_GetButtonImg(node);
	}
	if(this.child_count==0)
	{
		var button=get_obj("Tree_button_"+ this.id);
		if(button)  button.src=Tree_GetButtonImg(this);
		var expand = get_obj("Tree_expand_"+this.id);if(expand)expand.style.display="none";
		this.is_expanded=false;
	}
	if(child_node.next)
	{
		var button=get_obj("Tree_button_"+child_node.next.id);
		if(button)  button.src=Tree_GetButtonImg(child_node.next);
	}
}
/* ------------------------------------------------------------------------- */
/* 增加一个字节点                                                            */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.add_child=function (index,text,img1,img2,tip_text,status)
{
	var action=new Tree_Action("","","",this.action.treeViewIndex);
	var node=new Tree_Node(text,img1,img2,tip_text,status,action);
	this.__add_child(node,index);
	return node;
}
/* ------------------------------------------------------------------------- */
/* 判断一个节点是否为字节点                                                  */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.contain = function (test_node)
{
	if(test_node==null || this.get_tree().id != test_node.get_tree().id) return false;
	var i=test_node.get_level();
	var node = test_node;
	while(i>=1)
	{
		if(node.id==this.id) return true;
		node=node.parent;
		i--;
	}
	return false;
}
/* ------------------------------------------------------------------------- */
/* 删除一个字节点                                                            */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.del_child = function (index)
{
	this.__del_child(index);
}
/* ------------------------------------------------------------------------- */
/* 得到节点在同一父节点下的序号                                              */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.get_index = function ()
{
	if(this.parent == null) return -1;
	var index=0;
	var node = this.parent.first_child;
	while(node.id!=this.id)
	{
		node=node.next;
		index++;
	}
	return index;
}
/* ------------------------------------------------------------------------- */
/* 删除节点                                                                  */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.del = function ()
{
	if(this.parent)
	{
		var tree=this.get_tree();
		this.parent.del_child(this.get_index());
	}
}
/* ------------------------------------------------------------------------- */
/* 增加一个兄弟节点                                                          */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.add_sibling=function (index,text,img1,img2,tip_text, status)
{
	if(this.parent==null) return null;
	var n=0;
	var node=this.parent.first_child;
	while(node.id != this.id && node!=null)
	{
		n++;
		node=node.next;
	}
	if(index==TREE_PREV) index=n;
	if(index==TREE_NEXT) index=n+1;
	return this.parent.add_child(index,text,img1,img2,tip_text,status);
}
/* ------------------------------------------------------------------------- */
/* 设置节点的超链接                                                          */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.set_link=function (link,target)
{
	if(this.parent==null) return;
	this.action.type=TREE_LINK;
	this.action.target=target;
	this.action.link=link;
	this.action.script="";
}
/* ------------------------------------------------------------------------- */
/* 设置节点的脚本                                                            */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.set_script=function (script)
{
	this.action.type=TREE_SCRIPT;
	this.action.script=script;
	this.action.target="";
	this.action.link="";
}

/* ------------------------------------------------------------------------- */
/* 增加一个字节点                                                            */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.__add_child = function (child,index)
{
	var n=0;
	if(index==TREE_FIRST) index=0;
	if(index==TREE_LAST) index=this.child_count;
	if(index<0) index=0;
	if(index>this.child_count) index=this.child_count;
	var p1 = p2 = this.first_child;
	while(n<index && p1!=null)
	{
		n++;
		p2=p1;
		p1=p1.next;
	}
	if(p1 == this.first_child )
	{
		if(this.first_child == null)
		{
			this.first_child = this.last_child = child;
			this.first_child.next = this.first_child.prev = null;
			this.last_child.next = this.last_child.prev = null;
		}
		else
		{
			this.first_child = child; this.first_child.prev = null;
			this.first_child.next = p2; p2.prev = this.first_child;
		}
	}
	else if(p2 == this.last_child)
	{
		this.last_child = child; this.last_child.next= null;
		this.last_child.prev = p2; p2.next = child;
	}
	else
	{
		p2.next = child; child.prev = p2;
		child.next = p1; p1.prev = child;
	}
	child.parent = this;
	this.child_count++;
	this.cb__add_child(child);
}
/* ------------------------------------------------------------------------- */
/* 删除一个字节点                                                            */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.__del_child = function (index)
{
	if(this.child_count <=0 || index>this.child_count-1 ) return;
	var p,temp;
	var i=0;
	p=this.first_child;
	while(i<index&&p!=null&&p.next!=null)
	{
		i++;p=p.next;
	}
	if(p==this.first_child)
	{
		if(this.last_child==this.first_child)
		{
			this.last_child=this.first_child=null;
		}
		else
		{
			this.first_child=this.first_child.next;
			this.first_child.prev=null;
		}
		p.__is_first=true;
	}
	else if(p==this.last_child)
	{
		this.last_child=this.last_child.prev;
		this.last_child.next=null;
		p.__is_last=true;
	}
	else
	{
		temp=p.prev;temp.next=p.next;
		temp=p.next;temp.prev=p.prev;
	}
	this.child_count--;
	this.cb__del_child(p);
	Tree_Node_Array[p.id]=null;
}
/* ------------------------------------------------------------------------- */
/* 得到节点的属于第几层                                                      */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.get_level=function ()
{
	if(this.parent==null) return -1;
	var tmp=this;var n=0;
	while(tmp && tmp.parent)  
	{
		n++;
		tmp=tmp.parent;
	}
	return n;
}
/* ------------------------------------------------------------------------- */
/* 得到节点的第几层父节点                                                    */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.get_parent_ex=function (level)
{
	var p=this;var n=this.get_level();
	if(n<1)return null;
	while(p && level!=n)
	{
		p=p.parent;n--;
	}
	return p;
}
/* ------------------------------------------------------------------------- */
/* 得到节点以及子节点的HTML脚本                                              */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.getHtml=function ()
{
	var html="";
	if(this.parent)
	{
		var tree=this.get_tree();
		html+="<DIV>";
		html+=Tree_table(this);
		html+="<DIV id=Tree_expand_"+this.id+" STYLE='{display:"+(this.is_expanded?"block;":"none;")+"}'>";
	}
	if(this.child_count>0) 
	{
		for(var node=this.first_child;node;node=node.next)
		{
			html+=node.getHtml();
		}
	}
	if(this.parent) html+="</DIV></DIV>";
	return html;
}
/* ------------------------------------------------------------------------- */
/* 刷新节点以及子节点的HTML脚本                                              */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.refresh=function ()
{
	var obj=get_obj("Tree_expand_"+this.id);
	var html="";
	if(obj && this.parent)
	{
		var tree=this.get_tree();
		html+=Tree_table(this);
		html+="<DIV id=Tree_expand_"+this.id+" STYLE='{display:"+(this.is_expanded?"block;":"none;")+"}'>";
		if(this.child_count>0) 
		{
			for(var node=this.first_child;node;node=node.next)
			{
				html+=node.getHtml();
			}	
		}
		html+="</DIV>";
		obj.parentNode.innerHTML=html;
		if(this.is_selected)this.select();else this.unselect();
	}
}

Tree_Node.prototype.__refresh=function ()
{
	if(this.parent) return;
	var div=get_obj("Tree_View_"+this.get_tree().id);
	div.innerHTML=this.getHtml();
	var tree_id = this.get_tree().id;
	for(var i=(Tree_Node_Array.length)-1;i>=0 ;i--)
	{
		var n=Tree_Node_Array[i];
		if(n&&n.action.treeViewIndex==tree_id)
		{
			if(n.is_selected)n.select();else n.unselect();
		}
	}
}
/* ------------------------------------------------------------------------- */
/* 执行节点的动作                                                            */
/* ------------------------------------------------------------------------- */
Tree_Node.prototype.do_action = function()//!!!!!!!!!!!!!
{
	var action = this.action;
	if(action.type==TREE_LINK)//link
	{
		var url=action.link;
		var target = action.target;
		if(target == null || target=="")//default target
			target = Tree_View_Array[action.treeViewIndex].target;
		if(url!="") window.open(url,target);
	}
	else if(action.type==TREE_SCRIPT)
	{//run script
		eval(action.script);
	}
}